$(function() {
	inifo(GetQueryString("id"));
	comment(GetQueryString("id"));
	$("#replyadd").click(function() {
		addReply(GetQueryString("id"));
	})
})
function GetQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) return unescape(r[2]);
	return null;
}


function inifo(id) {
	$.ajax({
		//"url" : "http://127.0.0.1/app/exp/lookInfo",
		"url" : Server + "/app/exp/lookInfo",
		"type" : "POST",
		"data" : {
			"id" : id
		},
		success : function(data) {
			var m = data;
			var status1 = "";
			var avgScore = (m.comment.avgScore == null)?0:m.comment.avgScore;
			//0:正常、1:损坏、2:需更换耗材
			switch (m.insStatus) {
			case 0:
				status1 = "正常";
				break;
			case 1:
				status1 = "损坏";
				break;
			case 2:
				status1 = "需更换耗材";
				break;

			}
			//alert(m.times);
			$("#time").html(m.times);
			$("#name").html(m.chineseName);
			$("#model").html(m.insInfo.serial);
			$("#imageSrc").attr("src", m.insInfo.infoSrc);
			//$("#score").html(m.avenger);
			//$("#content").html(m.content);
			$("#status").html(status1);
		//$("#time1").html(m.time);
			$.fn.raty.defaults.path = 'images';
			$('#readOnly').raty({
				readOnly : true,
				score : avgScore
			});
		}
	})
}
function comment(id) {
	$.ajax({
		//"url" : "http://127.0.0.1/app/exp/lookComment",
		"url" : Server + "/app/exp/lookComment",
		"type" : "POST",
		"data" : {
			"soId" : id
		},
		success : function(data) {
			var w = data;
			$("#score").html(w.avenger);
			$("#content").html(w.content);
			$("#time1").html(w.time);
		}
	})
}
function addReply(id) {
	var content = $("#reply").val();
	var userid = $('#useridoflogin').val();
	var text = $('#content').val();
	var rate = (text == '') ? 2 : 3;
	$.ajax({
		//"url" : "http://127.0.0.1/app/exp/savetalk",
		"url" : Server + "/app/exp/savetalk",
		"type" : "Post",
		"data" : {
			"uId" : userid,
			"soId" : id,
			"content" : content,
			"type" : 2,
		},
		success : function(data) {
			$.ajax({
				//"url":"http://127.0.0.1/app/exp/modifyStatus",
				url : Server + "/app/exp/modifyStatus",
				type : "POST",
				data : {
					"id" : id,
					"status" : 4,
					"rate" : rate,
				},
				success : function(data) {
					alert("添加成功");
					$(location).attr('href', 'book_admin25');
				},
				error : function(msg) {
					alert("修改失败，请联系管理员！");
				}
			})
		}
	})
}

/*$.fn.raty.defaults.path = 'images';
$('#readOnly').raty({
	readOnly : true,
	score : 4.5
});*/

$(function() {
	$('#default').raty();

	$('#score').raty({
		score : 3
	});

	$('#score-callback').raty({
		score : function() {
			return $(this).attr('data-score');
		}
	});

	$('#scoreName').raty({
		scoreName : 'entity[score]'
	});

	$('#number').raty({
		number : 10
	});

	$('#number-callback').raty({
		number : function() {
			return $(this).attr('data-number');
		}
	});

	$('#numberMax').raty({
		numberMax : 5,
		number : 100
	});

	$('#starHalf').raty({
		half : true,
		path : null,
		starHalf : 'images/star-half-mono.png',
		starOff : 'images/star-off.png',
		starOn : 'images/stars-all.png'
	});


	$('#size').raty({
		path : 'images',
		cancel : true,
		cancelOff : 'cancel-off.png',
		cancelOn : 'cancel-on.png',
		half : true,
		starHalf : 'star-half.png',
		starOff : 'star-off.png',
		starOn : 'stars-all.png'
	});

	$('#target-div').raty({
		cancel : true,
		target : '#target-div-hint'
	});

	$('#target-text').raty({
		cancel : true,
		target : '#target-text-hint'
	});

	$('#target-textarea').raty({
		cancel : true,
		target : '#target-textarea-hint'
	});

	$('#target-select').raty({
		cancel : true,
		target : '#target-select-hint'
	});

	$('#targetType').raty({
		cancel : true,
		target : '#targetType-hint',
		targetType : 'score'
	});

	$('#targetKeep').raty({
		cancel : true,
		target : '#targetKeep-hint',
		targetKeep : true
	});

	$('#targetText').raty({
		target : '#targetText-hint',
		targetText : '--'
	});

	$('#targetFormat').raty({
		target : '#targetFormat-hint',
		targetFormat : 'Rating: {score}'
	});

	$('#targetScore').raty({
		targetScore : '#target-score'
	});


	$('#precision').raty({
		cancel : true,
		cancelOff : 'cancel-off.png',
		cancelOn : 'cancel-on.png',
		path : 'images',
		starHalf : 'star-half.png',
		starOff : 'star-off.png',
		starOn : 'stars-all.png',
		target : '#precision-hint',
		targetKeep : true,

		precision : true
	});


	$('#functions').raty({
		cancel : true,
		cancelOff : 'cancel-off.png',
		cancelOn : 'cancel-on.png',
		path : 'images',
		precision : true,
		starHalf : 'star-half.png',
		starOff : 'star-off.png',
		starOn : 'stars-all.png',
		target : '#functions-hint',
		targetKeep : true,
		click : function(score, evt) {
			alert('ID: ' + this.id + "\nscore: " + score + "\nevent: " + evt.type);
		},
		hints : [
			[ 'bad 1', 'bad 2', 'bad 3', 'bad 4', 'bad 5', 'bad 6', 'bad 7', 'bad 8', 'bad 9', 'bad' ],
			[ 'poor 1', 'poor 2', 'poor 3', 'poor 4', 'poor 5', 'poor 6', 'poor 7', 'poor 8', 'poor 9', 'poor' ],
			[ 'regular 1', 'regular 2', 'regular 3', 'regular 4', 'regular 5', 'regular 6', 'regular 7', 'regular 8', 'regular 9', 'regular' ],
			[ 'good 1', 'good 2', 'good 3', 'good 4', 'good 5', 'good 6', 'good 7', 'good 8', 'good 9', 'good' ],
			[ 'gorgeous 1', 'gorgeous 2', 'gorgeous 3', 'gorgeous 4', 'gorgeous 5', 'gorgeous 6', 'gorgeous 7', 'gorgeous 8', 'gorgeous 9', 'gorgeous' ]
		]
	});

	$('#score-function-run').on('click', function() {
		$('#functions').raty('score', $('#' + this.id.replace('-run', '')).val());
	});

	$('#click-function-run').on('click', function() {
		$('#functions').raty('click', $('#' + this.id.replace('-run', '')).val());
	});

	$('#readOnly-function-run').on('click', function() {
		var isReadOnly = $('#' + this.id.replace('-run', '')).val() === 'true';

		$('#functions').raty('readOnly', isReadOnly);
	});

	$('#cancel-function-run').on('click', function() {
		var isTrigger = $('#' + this.id.replace('-run', '')).val() === 'true';

		$('#functions').raty('cancel', isTrigger);
	});

	$('#reload-function-run').on('click', function() {
		$('#functions').raty('reload');
	});

	$('#get-score-function-run').on('click', function() {
		alert('score: ' + $('#functions').raty('score'));
	});

	$('#set-score-function-run').on('click', function() {
		$('#functions').raty('score', $('#' + this.id.replace('-run', '')).val());
	});

	$('#set-function-run').on('click', function() {
		var options = $('#' + this.id.replace('-run', '').val()),
			command = "$('#functions').raty('set', " + options + ");";

		eval(command);
	});

	$('#destroy-function-run').on('click', function() {
		$('#functions').raty('destroy');
	});

	$('#move-function-run').on('click', function() {
		$('#functions').raty('move', $('#' + this.id.replace('-run', '')).val());
	});
});